package cs271.examples.priorityqueue;

import java.util.Comparator;
import java.util.PriorityQueue;

public class JavaCollectionsPriorityQueue {

	public static void main(String[] args) {
		PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
		
		heap.offer(9);
		heap.offer(10);
		heap.offer(10);
		heap.offer(5);
		
		//notice here that we can have duplicates in a heap
		while(heap.size()>0)
			System.out.println(heap.remove());
		
		PriorityQueue<String> stringHeap = new PriorityQueue<String>();
		
		stringHeap.offer("z");
		stringHeap.offer("a");
		stringHeap.offer("A");
		stringHeap.offer("q");
		stringHeap.offer("Q");
		stringHeap.offer("m");
		
		
		System.out.println();
		//The object's compareTo method is used to determine the priority of the 
		//object.  So in this case, we will see the strings printed in
		//lexographic order. (capitals come first)
		while(stringHeap.size()>0)
			System.out.println(stringHeap.remove());
		
		stringHeap = new PriorityQueue<String>(11, new Comparator<String>(){
			public int compare(String o1, String o2) {
				//return o2.compareTo(o1);
				//same as:
				return -1*o1.compareTo(o2);
			}
			
		});
		
		PriorityQueue<Integer> myHeap = new PriorityQueue<Integer>(11, new Comparator<Integer>(){
			public int compare(Integer o1, Integer o2) {
				//return o2.compareTo(o1);
				//same as:
				return -1*o1.compareTo(o2);
			}
			
		});
		
		stringHeap.offer("z");
		stringHeap.offer("a");
		stringHeap.offer("A");
		stringHeap.offer("q");
		stringHeap.offer("Q");
		stringHeap.offer("m");
		
		myHeap.add(3);
		myHeap.add(4);
		myHeap.add(5);
		
		
		System.out.println();
		//The object's compareTo method is used to determine the priority of the 
		//object.  So in this case, we will see the strings printed in
		//lexographic order. (capitals come first)
		while(myHeap.size()>0)
			System.out.println(myHeap.remove());

	}

}
